package system.student.login.impl;

import system.student.Context.LogContext;
import system.student.entity.login.LoginResponse;
import system.student.entity.user.Student;
import system.student.entity.user.Teacher;
import system.student.entity.user.UserInfo;
import system.student.entity.user.UserType;
import system.student.mapper.StudentMapper;
import system.student.mapper.TeacherMapper;
import system.student.util.LogRecordUtil;
import system.student.util.MapperUtil;

public class LoginStrategy {
    private static final TeacherMapper teacherMapper = MapperUtil.getMapper(TeacherMapper.class);
    private static final StudentMapper studentMapper = MapperUtil.getMapper(StudentMapper.class);


    public static LoginResponse getLoginResponse(UserInfo user) {
        LogContext.setContext(new LogContext.LogContextData(user.getId(), user.getUsername()));
        // 根据用户类型创建不同的响应
        LoginResponse loginResponse;
        switch (user.getUserType()) {
            case Teacher:
                TeacherMapper proxyTeacherMapper = LogRecordUtil.wrap(teacherMapper, TeacherMapper.class);
                Teacher teacher = proxyTeacherMapper.selectByUserId(user.getId());
                teacher.setUserInfo(user);
                loginResponse = new LoginResponse(UserType.Teacher, teacher);
                break;
            case Student:
                StudentMapper proxyStudentMapper = LogRecordUtil.wrap(studentMapper, StudentMapper.class);
                Student student = proxyStudentMapper.selectByUserId(user.getId());
                student.setUserInfo(user);
                loginResponse = new LoginResponse(UserType.Student, student);
                break;
            case Admin:
                loginResponse = new LoginResponse(UserType.Admin, user);
                break;
            default:
                throw new RuntimeException("未知用户类型");
        }
        System.out.println("登录成功");
        return loginResponse;
    }
}
